﻿using EYangFrame.DataAccess.Global;
using EYangFramewirk.Business.MessageSystem.Interfaces;
using EYangFramework.Common.Ninject;
using EYangFramework.Domain.Entitys.Entrys;
using EYangFramework.Domain.Entitys.ValueObjects;
using SMES.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using Message = EYangFramework.Domain.Entitys.Entrys.Message;

namespace EYangFramework.MessageSystem
{
    public partial class frm_SendMessage : EYangFramework.CommonForm.frm_BasicForm
    {
        List<Role> Roles;
        List<LinkUser> LinkUsers;
        List<User> Users;
        public frm_SendMessage()
        {
            this.AllowSingtonTimer = false;
            InitializeComponent();
        }

        private void frm_SendMessage_Load(object sender, EventArgs e)
        {
            SetTabVisible(tp_Detail, false);
            BindModel();
            cheAllUser_CheckedChanged(null, null);
        }
        private void BindModel()
        {
            listModel.Items.Clear();
            using (IBLLSendMessage bLLSendMessage = NinjectProvider.NinjectCore.GetService<IBLLSendMessage>())
            {
                List<MessageModel> models = bLLSendMessage.GetMessageModel();
                Roles = bLLSendMessage.GetRoles();
                LinkUsers = bLLSendMessage.GetLinkUsers();
                Users = bLLSendMessage.GetUsers();
                foreach (var item in models)
                {
                    listModel.Items.Add(item.ModelName);
                }
            }
        }

        private void cheAllUser_CheckedChanged(object sender, EventArgs e)
        {
            tv_LinkUser.Nodes.Clear();
            if (cheAllUser.Checked)
            {
                foreach (var parent in Roles)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = parent.ROLENAME;
                    var childrens = parent.USERS;
                    foreach (var child in childrens)
                    {
                        TreeNode leaf = new TreeNode();
                        leaf.Text = child.USERNAME;
                        leaf.Tag = "U";
                        tn.Nodes.Add(leaf);
                    }
                    tv_LinkUser.Nodes.Add(tn);
                }
            }
            else
            {
                foreach (var parent in LinkUsers)
                {
                    TreeNode tn = new TreeNode();
                    tn.Text = parent.Name;
                    string[] childrens = parent.LinkUsers.Split(',');
                    foreach (var child in childrens)
                    {
                        TreeNode leaf = new TreeNode();
                        leaf.Text = child;
                        leaf.Tag = "U";
                        tn.Nodes.Add(leaf);
                    }
                    tv_LinkUser.Nodes.Add(tn);
                }
            }
            tv_LinkUser.ExpandAll();
        }

        private void listModel_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listModel.SelectedItem != null)
            {
                using (IBLLSendMessage bLLSendMessage = NinjectProvider.NinjectCore.GetService<IBLLSendMessage>())
                {
                    richMessage.Text = bLLSendMessage.GetModelByName(listModel.SelectedItem.ToString()).MessageModelContent;
                }
            }
        }

        private void treeLinkUser_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
            {
                foreach (TreeNode item in e.Node.Nodes)
                {
                    item.Checked = e.Node.Checked;
                }
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(richMessage.Text))
            {
                MessageBox.Show("请编辑需要发送的消息内容!");
                return;
            }
            if (cheSendEmail.Checked && string.IsNullOrEmpty(txtSubject.Text))
            {
                MessageBox.Show("需要发送邮件,必须填写邮件主题!");
                return;
            }
            List<string> selectedUser = new List<string>();
            Message sys_Message = new Message();
            sys_Message.CREATEUSER = GlobalInfo.CurrentUser.USERNAME;
            sys_Message.IsSendEmail = cheSendEmail.Checked ? "1" : "0";
            sys_Message.MessageContent = richMessage.Text;
            sys_Message.From = GlobalInfo.CurrentUser.USERNAME;
            foreach (TreeNode node in tv_LinkUser.Nodes)
            {
                selectedUser.AddRange(GetSelectdUsers(node));
            }
            sys_Message.To = string.Join(",", selectedUser.Except(new List<string>() { GlobalInfo.CurrentUser.USERNAME }));

            using (IBLLSendMessage bLLSendMessage = NinjectProvider.NinjectCore.GetService<IBLLSendMessage>())
            {
                bLLSendMessage.SendMessage(sys_Message);
            }
            if (cheSendEmail.Checked)
            {
                List<string> Emails = new List<string>();
                foreach (var item in selectedUser)
                {
                    Emails.Add(Users.Where(u => u.USERNAME == item).First().EMAIL);
                }
                string toEmail = String.Join(";", Emails);
                string passwordEmail = ConfigurationManager.AppSettings["PasswordEmail"];
                EmailSendHelper eh = new EmailSendHelper(EmailServerProvider.Mail163ExSmtpAddress,
                    EmailServerProvider.Mail163ExSmtpPort, GlobalInfo.CurrentUser.EMAIL, toEmail, passwordEmail);
                eh.SendMessage(txtSubject.Text, richMessage.Text);
            }
            MessageBox.Show("消息发送成功,请关注相关工程师的回复!");
            Thread.Sleep(500);
            this.Close();
        }
        private List<string> GetSelectdUsers(TreeNode treeNode)
        {
            List<string> selectedUser = new List<string>();
            if (treeNode.Checked && treeNode.Tag != null)
            {
                selectedUser.Add(treeNode.Text);
            }
            if (treeNode.Nodes.Count > 0)
            {
                foreach (TreeNode item in treeNode.Nodes)
                    selectedUser.AddRange(GetSelectdUsers(item));
            }
            return selectedUser;
        }
    }
}
